

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using JTC.MiniCommerce.Core;
using JTC.MiniCommerce.Localization;
using JTC.MiniCommerce.Store;
using SubSonic.Utilities;

namespace JTC.MiniCommerce.Web.admin {
  public partial class notificationedit : System.Web.UI.Page {

    #region Member Variables

    private int notificationId = 0;
    private Notification notification;
    
    #endregion
    
    #region Page Events

    /// <summary>
    /// Raises the <see cref="E:System.Web.UI.Control.Init"></see> event to initialize the page.
    /// </summary>
    /// <param name="e">An <see cref="T:System.EventArgs"></see> that contains the event data.</param>
    protected override void OnInit(EventArgs e) {
      Session["FCKeditor:UserFilesPath"] = @"~/repository/content/";
      base.OnInit(e);
    }

    /// <summary>
    /// Handles the Load event of the Page control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="T:System.EventArgs"/> instance containing the event data.</param>
    protected void Page_Load (object sender, EventArgs e) {
      try {
      notificationId = Utility.GetIntParameter("notificationId");
      notification = new Notification(notificationId);
      SetNotificationEditProperties();
      if (!Page.IsPostBack) {
        if (notificationId > 0) {
          chkIsSystemNotification.Checked = notification.IsSystemNotification;
          txtName.Text = notification.Name;
          txtToList.Text = notification.ToList;
          txtCcList.Text = notification.CcList;
          txtFromName.Text = notification.FromName;
          txtFromEmail.Text = notification.FromEmail;
          txtSubject.Text = notification.Subject;
          chkIsHtml.Checked = notification.IsHTML;
          txtNotificationBody.Value = HttpUtility.HtmlDecode(notification.NotificationBody);
          lblNotificationEdit.Text = LocalizationUtility.GetText("lblNotificationEdit") + " >> " + notification.Name;
        }
      }
      }
      catch (Exception ex){
        Logger.Error(typeof(notificationedit).Name + ".Page_Load", ex);
        Master.MessageCenter.DisplayCriticalMessage(LocalizationUtility.GetCriticalMessageText(ex.Message));
      }
    }

    /// <summary>
    /// Handles the Click event of the btnSave control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="T:System.EventArgs"/> instance containing the event data.</param>
    protected void btnSave_Click (object sender, EventArgs e) {
      try {
        notification.Name = txtName.Text;
        notification.ToList = txtToList.Text;
        notification.CcList = txtCcList.Text;
        notification.FromName = txtFromName.Text;
        notification.FromEmail = txtFromEmail.Text;
        notification.Subject = txtSubject.Text;
        notification.IsHTML = chkIsHtml.Checked;
        notification.NotificationBody = HttpUtility.HtmlEncode(txtNotificationBody.Value);
        notification.Save(WebUtility.GetUserName());
        Master.MessageCenter.DisplaySuccessMessage(LocalizationUtility.GetText("lblNotificationSaved"));
      }
      catch (Exception ex) {
        Logger.Error(typeof(notificationedit).Name + ".btnSave_Click", ex);
        Master.MessageCenter.DisplayCriticalMessage(LocalizationUtility.GetCriticalMessageText(ex.Message));
      }
    }
    
    #endregion
    
    #region Methods
    
    #region Private

    /// <summary>
    /// Sets the notification edit properties.
    /// </summary>
    private void SetNotificationEditProperties () {
      LocalizationUtility.AddLabelText(lblIsSystemNotification);
      LocalizationUtility.AddLabelText(lblName);
      LocalizationUtility.AddLabelText(lblToList);
      LocalizationUtility.AddLabelText(lblCcList);
      LocalizationUtility.AddLabelText(lblFromName);
      LocalizationUtility.AddLabelText(lblFromEmail);
      LocalizationUtility.AddLabelText(lblSubject);
      LocalizationUtility.AddLabelText(lblIsHtml);
      LocalizationUtility.AddLabelText(lblNotificationBody);
      LocalizationUtility.AddPanelText(pnlNotificationDetails);
      LocalizationUtility.AddButtonText(btnSave);
      this.Title = LocalizationUtility.GetText("titleEditNotification");
    }

    #endregion
    
    #endregion

  }
}
